package apibasej.bkp.model;

import java.sql.DriverManager;

import sun.jdbc.odbc.JdbcOdbcDriver;
import apibasej.db.metadata.Col;
import apibasej.db.metadata.Tab;
import apibasej.db.server.utilmetadata.GenModelImage;
import apibasej.db.server.utilmetadata.LoaderModelDB;

public class GeneratorModelBkp {

	
	@SuppressWarnings({"unchecked","rawtypes"})
	public static void main(String[] args) throws Exception {
		
		
		DriverManager.registerDriver(new JdbcOdbcDriver());
		
		String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=/INFO_BKPS_FULL.mdb;}";// lê o dbf para gerar o model na raiz da partição atual
		LoaderModelDB l = new LoaderModelDB(url, null, null);
		
		l.setReplaceClass(Double.class,Long.class);// length e lastModified são long no java, mas para caber no access o col precisa ser Double
		l.setIgnorePksAndFks(true);//access não suporta ... preencher pks e fks
		l.load();
		
		// *********** seta PKs e FKs na mão ...****************
		Col idBkp = l.getTab(INFO_BKPS.BACKUP.getName()).getCol(BACKUP.ID.getName());
		idBkp.setPk(true);
		Tab tabFileInfo = l.getTab(INFO_BKPS.FILE_INFO.getName());
		Col idFileInfo = tabFileInfo.getCol(FILE_INFO.ID.getName());
		idFileInfo.setPk(true);
		tabFileInfo.getCol(FILE_INFO.ID_BACKUP.getName()).setForeignCol( idBkp );
		tabFileInfo.getCol(FILE_INFO.ID_FILE_REFERENCE.getName()).setForeignCol( idFileInfo );
		// ****************************************************
		
		String pathBin = GeneratorModelBkp.class.getResource("/").toString();
		String pathSrc = pathBin.substring(6,pathBin.length()-4)+"src/";
		System.out.println("pathSrc: "+pathSrc);
		System.out.println("package: "+GeneratorModelBkp.class.getPackage().getName());
		
		
		GenModelImage g = new GenModelImage(pathSrc, GeneratorModelBkp.class.getPackage().getName(),"INFO_BKPS", l);
		
		g.generate();
		
		System.out.println("*** PREENCHER PKS E FKS !!! ***");
		
	}
	
}
